<?
if( !$PARAPARA || !$PARAPARA->is_admin() )
    return;


//- functions -------------------------------------------------

function echo_info( $info_text ) {
    echo '<div class="padm_info">'; 
    echo $info_text;
    echo "</div>\n";
}

function echo_account_labels() {
    echo '<tr class="padm_acc_acc_labels">' . "\n";
    echo '<td class="padm_acc_acc_label">' . 'UID' . "</td>\n";
    echo '<td class="padm_acc_acc_label">' . 'Screen Name' . "</td>\n";
    echo '<td class="padm_acc_acc_label">' . 'E-mail' . "</td>\n";
    echo '<td class="padm_acc_acc_label">' . 'State' . "</td>\n";
    echo '<td class="padm_acc_acc_label">' . 'Group name' . "</td>\n";
    echo '<td class="padm_acc_acc_label">' . 'Karma' . "</td>\n";
    echo '<td class="padm_acc_acc_label">' . 'Created' . "</td>\n";
    echo "</tr>\n";
}

function echo_account( $acc ) {

    $edit_link = "admin.php?sec=acc&ssec=edit&uid=" . $acc->uid;

    echo '<tr class="padm_acc_acc">' . "\n";
    echo '<td class="padm_acc_acc_uid">' . $acc->uid . "</td>\n";
    echo '<td class="padm_acc_acc_screenname">' . "<a href=\"$edit_link\" >" . $acc->screen_name . "</a></td>\n";
    echo '<td class="padm_acc_acc_email">' . $acc->email . "</td>\n";
    echo '<td class="padm_acc_state">' .$acc->state . "</td>\n";
    echo '<td class="padm_acc_group">' .$acc->group_name . "</td>\n";
    echo '<td class="padm_acc_karma">' .$acc->karma . "</td>\n";
    echo '<td class="padm_acc_date">' .$acc->createdate . "</td>\n";
    echo "</tr>\n";
}

function echo_account_info( $info ) {
    echo '<tr class="padm_acc_accinfo">' . "\n";
    echo '<td colspan="7">' . $info . "</td>\n";
    echo '</tr>';
}


function echo_account_infocap( $info ) {
    echo '<tr class="padm_acc_accinfocap">' . "\n";
    echo '<td colspan="7">' . $info . "</td>\n";
    echo '</tr>';
}


//- the logic -------------------------------------------------

$do = $_GET['do'];
$dbl = $PARAPARA->dblayer;

$found_by_id;
$found_by_screenname;
$found_count = 0;

$sstring = '';

if($do == 'search') {
    $sstring = trim( $_GET['sstring'] );
    if( ($sstring == null) || ($sstring == '') ) {
        echo_info( "Type a search string" );
    }
    else {
        if( is_numeric($sstring) )  {
            $found_by_id = $dbl->account_fetch_by_uid( $sstring ); 
            if($found_by_id)
                $found_count++;
        }
        $found_by_screenname = $dbl->account_search_by_screenname( $sstring );
        if($found_by_screenname)
            $found_count += sizeof( $found_by_screenname );

        $output_search_results = true;   
        
    }
}

?>

<h2>Account search</h2>

<form action="?sec=acc&ssec=search&do=search" method="GET">
<input type="hidden" name="sec"  value="acc" />
<input type="hidden" name="ssec" value="search" />
<input type="hidden" name="do"   value="search" />
<div class="padm_acc_searchbox">

<div class="padm_acc_searchbox_value">
<input type="text" class="text" name="sstring" value="<? echo htmlspecialchars( $sstring ) ?>" />
</div>
<div class="padm_acc_searchbox_button">
<input type="submit" class="button" name="ssubmit" value="Search" />
</div>
</div>
</form>

<?
if($output_search_results) {
    echo '<table class="padm_acc_searchresults">';
    if($found_count) {
        echo_account_infocap( "Found $found_count result(s)");
        if( $found_by_id ) {
            echo_account_info( "Account with uid==" . htmlspecialchars( $sstring ));
            echo_account_labels();
            echo_account( $found_by_id );
        }

        if( $found_by_screenname ) {
            echo_account_info( "Account(s) containing '". htmlspecialchars( $sstring ). "'" );
            echo_account_labels();
            foreach( $found_by_screenname as $acc )
                echo_account( $acc );
        }
    }
    else
        echo_account_info("The search did not return anything");
    echo "</table>\n";
}

?>
